#include "glwidget.h"
#include <QDebug>

GLWidget::GLWidget()
{

}

GLWidget::~GLWidget()
{
    makeCurrent();
    teardownGL();
}


void GLWidget::initializeGL()
{
    /*初始化OpenGL后台*/
    initializeOpenGLFunctions();
    printContextInformation();

    /*设置全局信息*/
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

void GLWidget::resizeGL(int width, int height)
{
    (void)width;
    (void)height;
}

void GLWidget::paintGL()
{
    /*使用glClearColor设置的颜色来清除背景颜色*/
    glClear(GL_COLOR_BUFFER_BIT);
}

/**
 * @brief GLWidget::teardownGL  清除资源
 */
void GLWidget::teardownGL()
{

}

/**
 * @brief GLWidget::printContextInformation 打印OpenGL上下文信息
 */
void GLWidget::printContextInformation()
{
    QString type;
    QString version;
    QString profile;

    /*获取OpenGL版本信息*/
    type = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
    version = reinterpret_cast<const char*>(glGetString(GL_VERSION));

    /*获取OpenGL属性信息*/
#define CASE(c) case QSurfaceFormat::c: profile = #c; break
  switch (format().profile())
  {
    CASE(NoProfile);
    CASE(CoreProfile);
    CASE(CompatibilityProfile);
  }
#undef CASE

    qDebug() << qPrintable(type) << qPrintable(version) << "(" << qPrintable(profile) << ")";
}
